package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.common.RedisConst;
import com.itheima.health.dto.OrderMessageDTO;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.vo.OrderSubmitParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * 体检预约控制器
 */
@RestController
@RequestMapping("/mobile/order")
@Slf4j
public class MobileOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderSettingService orderSettingService;


    /**
     * 预约提交
     *
     * @param orderSubmitParam 体检预约信息
     */
    @PostMapping("/submit")
    public Result saveSetmealOrder(@RequestBody OrderSubmitParam orderSubmitParam) {
        log.info("体检预约信息 ===> {}", orderSubmitParam);
        //获取体检日期
        Date orderDate = orderSubmitParam.getOrderDate();
        //先判断体检日期是否为空
        if (orderDate == null) {
            return new Result(false, "体检日期不能为空！");
        }
        //判断当天是否可以预约
        Integer canOrder = orderSettingService.countCanOrder(orderDate);
        //不可以预约
        if (canOrder == 0) {
            return new Result(false, MessageConst.SELECTED_DATE_CANNOT_ORDER);
        }
        //判该用户预约当天的人数是否已满
        Integer count = orderSettingService.countOrderSettingByDay(orderDate);
        //预约已满
        if (count == 0) {
            return new Result(false, MessageConst.ORDER_FULL);
        }
        //调用业务层添加预约订单,返回订单数据
        Order order = orderService.saveOrder(orderSubmitParam);
        //返回结果
        return new Result(true, MessageConst.ORDER_SUCCESS, order);
    }

    /**
     * 通过id查询体检预约订单详情
     */
    @GetMapping("/findById")
    public Result getOrder(Integer id){
        log.info("要查询的订单id ===> {}",id);
        //调用业务层，查询数据
        OrderMessageDTO orderMessageDTO=orderService.getOrderMessage(id);
        //返回数据
        return new Result(true, MessageConst.QUERY_ORDER_SUCCESS,orderMessageDTO);
    }
}
